//
//  DetailDetai.m
//  UIA段项目 墨家天气
//
//  Created by dllo on 15/9/14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DetailDetai.h"
#import "UIImageView+WebCache.h"
#import "NovelDataBase.h"
#import "CollectModel.h"
#import "CollectButton.h"
@interface DetailDetai ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic,copy)NSString *countStr;
@property (nonatomic,retain) CollectButton *collecButton;
@property (nonatomic,retain)UILabel *countLable;
@property (nonatomic,retain)UILabel *titleLable;

@property (nonatomic,retain)UIView *headerView;

@end

@implementation DetailDetai

- (void)dealloc
{
    [_countStr release];
    [_collecButton release];
    [_titleLable release];
    [_headerView release];
    [_array release];
    [_scroll release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.collecButton = [[CollectButton alloc]initWithFrame:CGRectMake(0 * OffWidth, 155 * OffHeight, 120* OffWidth, 25 * OffHeight)];
        
        [_collecButton addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
            }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self createScrollViewSubViews];
    [self addDownView];
    [self addHeaderView];
}


- (void)createScrollViewSubViews
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.scroll];
    [_scroll setContentSize:CGSizeMake([self.array count] * self.view.frame.size.width, 0)];
    _scroll.contentOffset = CGPointMake(self.row * self.view.frame.size.width, 0);
    [_scroll setPagingEnabled:YES];
    [_scroll setDelegate:self];
    [_scroll release];
    
    
    for (int i = 0; i < [self.array count]; i++) {
        UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 60 * OffWidth, self.view.frame.size.width, 450 * OffHeight)];
        [_scroll addSubview:myImage];
        [myImage setUserInteractionEnabled:YES];
        [myImage sd_setImageWithURL:[NSURL URLWithString:[[self.array objectAtIndex:i] objectForKey:@"image"]]];
        [myImage release];

    }
    
    
    
}
- (void)addDownView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 200 * OffHeight, self.view.frame.size.width, 200 * OffHeight)];
    view.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    [self.view addSubview:view];
    
    
    
    self.countStr = [NSString stringWithFormat:@"%ld/%ld",self.row + 1,self.array.count];
    self.countLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100 * OffWidth, 30 * OffHeight)];
    self.countLable.text = _countStr;
    self.countLable.textColor = [UIColor whiteColor];
    [view addSubview:_countLable];
    
    
    
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonBack.frame = CGRectMake(self.view.frame.size.width / 2 - 15 * OffWidth,  150 * OffHeight, 30 * OffWidth, 30 * OffHeight);
    [buttonBack setBackgroundImage:[UIImage imageNamed:@"cha.png"] forState:UIControlStateNormal];
    [buttonBack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonBack addTarget:self action:@selector(backkClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:buttonBack];
    
    
    [view addSubview:_collecButton];
    

    
    BOOL judge = [NovelDataBase selectCollectWith:[[self.array objectAtIndex:self.scroll.contentOffset.x / self.view.frame.size.width]objectForKey:@"image"]];
    
    if (judge == YES) {
        
        self.collecButton.collectImage.image = [UIImage imageNamed:@"yishoucang.png"];
        self.collecButton.collectLable.text = @"已收藏";
        
        
        
    }else{
        
        self.collecButton.collectImage.image = [UIImage imageNamed:@"shoucnag.png"];
        self.collecButton.collectLable.text = @"收藏";
        
    }

}
- (void)addHeaderView
{
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60 * OffHeight)];
    _headerView.backgroundColor = [UIColor blackColor];
    _headerView.alpha = 0.5;
    [self.view addSubview:_headerView];
    [_headerView release];
    
    
    
    
    self.titleLable = [[UILabel alloc]initWithFrame:CGRectMake(40 * OffWidth,20 * OffHeight, 300 * OffWidth ,30 * OffHeight)];
    self.titleLable.textColor = [UIColor whiteColor];
    self.titleLable.font = [UIFont boldSystemFontOfSize:15];
    self.titleLable.textAlignment = 1;   
    self.titleLable.text  = [[self.array objectAtIndex:self.row] objectForKey:@"title"];
    [self.view addSubview:self.titleLable];
    [self.titleLable release];
    
    
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.titleLable.text  = [[self.array objectAtIndex:scrollView.contentOffset.x / self.view.frame.size.width] objectForKey:@"title"];
    self.countStr = [NSString stringWithFormat:@"%.0f/%ld",scrollView.contentOffset.x / self.view.frame.size.width + 1,self.array.count];
    self.countLable.text = _countStr;
    BOOL judge = [NovelDataBase selectCollectWith:[[self.array objectAtIndex:self.scroll.contentOffset.x / self.view.frame.size.width]objectForKey:@"image"]];

    
    if (judge == YES) {
        
        self.collecButton.collectImage.image = [UIImage imageNamed:@"yishoucang.png"];
        self.collecButton.collectLable.text = @"已收藏";
        
        
    }else{
        
        self.collecButton.collectImage.image = [UIImage imageNamed:@"shoucnag.png"];
        self.collecButton.collectLable.text = @"收藏";
        
    }
    
  

}
- (void)backkClick:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)collect:(UIButton *)button
{
    
   
    BOOL judge = [NovelDataBase selectCollectWith:[[self.array objectAtIndex:self.scroll.contentOffset.x / self.view.frame.size.width]objectForKey:@"image"]];
    
        if (judge) {
            
            CollectModel *model = [[CollectModel alloc]init];
            model.imageStr = [[self.array objectAtIndex:self.scroll.contentOffset.x / self.view.frame.size.width] objectForKey:@"image"] ;
            [NovelDataBase deleteCollectWithModel:model];
            self.collecButton.collectImage.image = [UIImage imageNamed:@"shoucnag.png"];
            self.collecButton.collectLable.text = @"收藏";
            
            [model release];
            
        }else{
            
            NSString *image = [[self.array objectAtIndex:self.scroll.contentOffset.x / self.view.frame.size.width]objectForKey:@"image"];
            NSString *t = [[self.array objectAtIndex:self.scroll.contentOffset.x / self.view.frame.size.width] objectForKey:@"title"] ;
            CollectModel *model = [[CollectModel alloc]initWithCollectImageStr:image title:t];
            [NovelDataBase insertCollectWithModel:model];
            [model release];
            
            self.collecButton.collectImage.image = [UIImage imageNamed:@"yishoucang.png"];
            self.collecButton.collectLable.text = @"已收藏";
            
    
        
    }
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
